package com.example.noobpackage.tool

import android.content.Context
import android.content.SharedPreferences
import com.example.noobpackage.logic.model.ApplicationUser

object LocalUserUtil {
    private val userInfoSharedPreferences: SharedPreferences =
        MyApplication.context.getSharedPreferences(
            "userInfo",
            Context.MODE_PRIVATE
        )

    fun getUserInfo(): ApplicationUser? {
        val isLogin = userInfoSharedPreferences.getBoolean("isLogin", false)
        return if (isLogin) {
            val name = userInfoSharedPreferences.getString("name", "")!!
            val telephone = userInfoSharedPreferences.getString("telephone", "")!!
            val department = userInfoSharedPreferences.getInt("department", 0)!!
            val account = userInfoSharedPreferences.getLong("account", 0)!!
            ApplicationUser(name, department, telephone, account)
        } else {
            null
        }
    }

    fun storageUserInfo(userInfo: ApplicationUser) {
        val editor = userInfoSharedPreferences.edit()
        editor.apply {
            putBoolean("isLogin", true)
            putString("name", userInfo.name)
            putString("telephone", userInfo.telephoneNumber)
            putInt("department", userInfo.studentDepartment)
            putLong("account", userInfo.account)
            apply()
        }
    }

}